﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using CCF20Model;
using System.Windows;
using System.Windows.Controls;

namespace CCF20
{
    /// <summary>
    /// Interaction logic for RoleDetail.xaml
    /// </summary>
    public partial class RoleDetail : Window
    {
        PermissionModel pm;
        string permissionString;
        string currentRole;
        ObservableCollection<string> myList; 
        public RoleDetail(string roleName)
        {
            InitializeComponent();
            pm = new PermissionModel();
            this.CurrentRoleName.Text = string.Format("当前{0}分组权限", roleName);
            permissionString = pm.GetPermissionForRole(roleName);
            currentRole = roleName;
            if (permissionString != null)
            {
                myList = new ObservableCollection<string>(permissionString.Split(';'));
                
            }
            else
            {
                myList = new ObservableCollection<string>();
            }
            currentRolePermission.ItemsSource = myList;
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
           
        }
        private void ChangePermission_Click(object sender, RoutedEventArgs e)
        {
            string action = ((Button)sender).Tag.ToString();
            switch (action)
            {
                case "AddPermission":
                    if (null !=  permissionList.SelectedItem)
                    {
                        string temp = ((ListBoxItem)permissionList.SelectedItem).Content.ToString();
                        if (!myList.Contains(temp) && temp.Trim()!= string.Empty)
                        {
                            myList.Add(temp);
                        }
                       
                    }
                    break;
                case "RemovePermission":
                    if (null != currentRolePermission.SelectedItem)
                    {
                        myList.Remove((string)currentRolePermission.SelectedItem);

                    }
                    break;
                case "ConfirmPermission":
                    
                    string rolePermissionList = string.Empty;
                    foreach (var p in myList)
                    {
                        rolePermissionList = rolePermissionList + string.Format("{0};", p);
                    }
                    pm.AddPermissionToRole(currentRole, rolePermissionList);
                    MessageBox.Show("保存成功");
                    this.Close();
                    break;
                default:
                    break;

            }
            currentRolePermission.Items.Refresh();
        }

    }
}
